#include "imGraphics.h"

using namespace imGraphics;

LRESULT CALLBACK imGraphics::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
	switch(message) {
	case WM_SIZE:
		// Window was resized!
		RECT WinRect;
		if(GetWindowRect(hWnd, &WinRect)) {
			unsigned int W = WinRect.right - WinRect.left;
			unsigned int H = WinRect.bottom - WinRect.top;

			//g_GFX->SetRes(W, H);
			//g_GFX->Init();
		}
	break;
	case WM_DESTROY:
		PostQuitMessage(0);
	break;
	};
	return DefWindowProc(hWnd, message, wParam, lParam);
}

imWindow::imWindow(void) {
	m_Instance = NULL;
	m_WndHandle = NULL;
	SetSize(200, 200);
	SetMode(DisplayMode::DSP_WINDOW);
	m_strTitle = "Imagine Window";
}

imWindow::imWindow(HINSTANCE Instance) {
	m_Instance = NULL;
	m_WndHandle = NULL;
	SetSize(200, 200);
	SetMode(DisplayMode::DSP_WINDOW);
	m_strTitle = "Imagine Window";
	Init(Instance);
}

void imWindow::ConfigFromFile(Config* Cfg) {
	int w = Cfg->GetInt("r_width", 1024);
	int h = Cfg->GetInt("r_height", 768);
	DisplayMode m = (DisplayMode)Cfg->GetInt("r_displaymode", 1);

	SetSize(w, h);
	SetMode(m);
}

void imWindow::Close() {
	if(m_WndHandle == NULL) return;

	ShowWindow(m_WndHandle, SW_HIDE);
	DestroyWindow(m_WndHandle);
}

void imWindow::Init(HINSTANCE Instance) {
	m_Instance = Instance;
	Init();
}

void imWindow::Init() {
	if(m_Instance == NULL) return;
	if(m_WndHandle != NULL) Close();

	WNDCLASSEX wcex;

	wcex.cbSize			= sizeof(WNDCLASSEX); // The size of the structure
	wcex.style			= CS_HREDRAW | CS_VREDRAW; // The class style
	wcex.lpfnWndProc	= (WNDPROC)WndProc; // The window procedure callback
	wcex.cbClsExtra		= 0; // Extra bytes to allocate for this class
	wcex.cbWndExtra		= 0; // Extra bytes to allocate for this instance
	wcex.hInstance		= m_Instance; // Handle to the application instance
	wcex.hIcon			= 0; // Icon to associate with the application
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW); // The default cursor
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1); // The background colour
	wcex.lpszMenuName	= NULL; // The resource name for the menu
	wcex.lpszClassName	= "ImagineWindow"; // The class name being created
	wcex.hIconSm		= 0; // The handle to the small icon

	RegisterClassEx(&wcex);

	unsigned int WindowStyle = 0;

	RECT wr = {0, 0, m_iWidth, m_iHeight};    // set the size, but not the position

	switch(m_dDisplayMode) {
	case DSP_WINDOW:
		WindowStyle = WS_OVERLAPPEDWINDOW;
		AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);    // adjust the size
		break;
	case DSP_WINDOWNOBORDER:
	case DSP_FULLSCREEN:
		WindowStyle = WS_EX_TOPMOST | WS_POPUP | WS_VISIBLE;
		break;
	};

	m_WndHandle = CreateWindow(
		"ImagineWindow", // The window class to use
		m_strTitle.c_str(), // The title bar text
		WindowStyle, // The window style
		CW_USEDEFAULT, // The starting X coordinate
		CW_USEDEFAULT, // The starting Y coordinate
		wr.right - wr.left, // The pixel width of the window
		wr.bottom - wr.top, // The pixel height of the window
		NULL, // The parent window; NULL for desktop
		NULL, // The menu for the application; NULL for none
		m_Instance, // The handle to the application instance
		NULL); // No values passed to the window

	if(!m_WndHandle)
		return;

	ShowWindow(m_WndHandle, SW_SHOW);
	UpdateWindow(m_WndHandle);
}

void imWindow::SetSize(unsigned int iWidth, unsigned int iHeight) {
	m_iWidth = iWidth;
	m_iHeight = iHeight;
}

void imWindow::SetMode(DisplayMode dDisplayMode) {
	m_dDisplayMode = dDisplayMode;
}

void imWindow::SetTitle(std::string strTitle) {
	m_strTitle = strTitle;
}

HWND imWindow::GetHandle() {
	return m_WndHandle;
}

/*class imWindow {
private:
	unsigned int m_iWidth, m_iHeight;
	DisplayMode m_dDisplayMode;
	HWND m_WndHandle;
	HINSTANCE m_Instance;
	std::string m_strTitle;
public:
	imWindow();
	imWindow(HINSTANCE Instance);

	void Close();

	void Init();
	void Init(HINSTANCE Instance);
	void SetSize(unsigned int iWidth, unsigned int iHeight);
	void SetMode(DisplayMode dDisplay);
	void SetTitle(std::string strTitle);

	HWND GetHandle();
}; */